﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace LabMS.Web.Post.BBS
{
    public partial class PubishPost : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["plateID"]))
                {
                    int id = 0;

                    if (!int.TryParse(Request.QueryString["plateID"], out id))
                    {
                        throw new Exception("你所访问的页面不存在!");
                    }

                    LabMS.BLL.PostPlate pp = new LabMS.BLL.PostPlate();
                    LabMS.Model.PostPlate ppInfo = pp.GetModel(id);

                    if (ppInfo == null)
                    {
                        throw new Exception("你所访问的页面不存在!");
                    }

                    PlateName.Text = ppInfo.Name;
                }
                else
                {
                    throw new Exception("你所访问的页面不存在!");
                }
            }
        }

        protected void Page_Error(object sender, EventArgs e)
        {
            Exception ex = Server.GetLastError();
            WriteBackScript("alert('" + ex.Message + "')");
        }

        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            #region validation

            if (string.IsNullOrEmpty(PostTitle.Text.Trim())) { WriteBackScript("alert('交流主题不能为空!')"); return; }
            else if (PostTitle.Text.Trim().Length > 100) { WriteBackScript("alert('交流主题长度不能超过100字符!')"); return; }

            if (string.IsNullOrEmpty(PostContent.Text.Trim())) { WriteBackScript("alert('交流内容不能为空!')"); return; }
            else if (PostContent.Text.Length > 1000) { WriteBackScript("alert('交流内容长度不能超过1000!')"); return; }

            #endregion

            LabMS.BLL.Post post = new LabMS.BLL.Post();
            LabMS.Model.Post postInfo = new LabMS.Model.Post();

            postInfo.PostTitle = PostTitle.Text.Trim();
            postInfo.PostContent = Server.HtmlEncode(PostContent.Text);
            postInfo.PostRecordTime = DateTime.Now;
            postInfo.ViewTimes = 0;
            postInfo.PostPlateID = int.Parse(Request.QueryString["plateID"]);

            postInfo.PosterType = Base.PrivilegeManager.GetUserTypeCode();
            postInfo.PosterID = int.Parse(UserID);

            postInfo.ID = post.Add(postInfo);

            WriteBackScript("alert('发贴成功!');window.returnValue=true;window.opener.location.replace(window.opener.location);window.close();");
        }

        /// <summary>
        /// 写回脚本
        /// </summary>
        /// <param name="script">脚本内容</param>
        protected void WriteBackScript(string script)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
                Guid.NewGuid().ToString(), "<script type='text/javascript'>" + script + "</script>");
        }
    }
}
